﻿Imports System.Data.SqlClient

Public Class clsCot_Solicitud
    Private mintIdSolicitud As Integer
    Private mintFolio As Integer
    Private mdatFecha As Date
    Private mstrSolicita As String
    Private mintEstado As Integer
    Public Property IdSolicitud() As Integer
        Get
            Return mintIdSolicitud
        End Get
        Set(ByVal value As Integer)
            mintIdSolicitud = value
        End Set
    End Property
    Public Property Folio() As Integer
        Get
            Return mintFolio
        End Get
        Set(ByVal value As Integer)
            mintFolio = value
        End Set
    End Property
    Public Property Fecha() As Date
        Get
            Return mdatFecha
        End Get
        Set(ByVal value As Date)
            mdatFecha = value
        End Set
    End Property
   
    Public Property Solicita() As String
        Get
            Return mstrSolicita
        End Get
        Set(ByVal value As String)
            mstrSolicita = value
        End Set
    End Property
    Public Property Estado() As Integer
        Get
            Return mintEstado
        End Get
        Set(ByVal value As Integer)
            mintEstado = value
        End Set
    End Property
   
    Public Sub New()
        mintIdSolicitud = 0
        mintFolio = 0
        mdatFecha = Now()
        mstrSolicita = ""
        mintEstado = 0
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From Cot_Solicitud " & _
        " WHERE Folio=" & Folio

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then

            mdatFecha = rdBuscar("Fecha")
            mstrSolicita = rdBuscar("Solicita")
            mintEstado = rdBuscar("Estado")
        Else

            mdatFecha = Now()
            mstrSolicita = ""
            mintEstado = 0
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into Cot_Solicitud (" & _
                            "Folio," & _
                            "Fecha," & _
                            "Solicita," & _
                            "Estado)" & _
                            "VALUES (" & _
                            "@Folio," & _
                            "@Fecha," & _
                            "substring(@Solicita,1,300)," & _
                            "@Estado)" & _
                            " SELECT SCOPE_IDENTITY()"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("Folio", mintFolio)
        Command.Parameters.AddWithValue("Fecha", mdatFecha)
        Command.Parameters.AddWithValue("Solicita", mstrSolicita)
        Command.Parameters.AddWithValue("Estado", mintEstado)

        cnConn.Open()
        IdSolicitud = Convert.ToInt32(Command.ExecuteScalar)

        If IdSolicitud > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar la solicitud ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
